package com.test.deencredit.fragment_deencredit.ln_state_deencredit;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.deencredit.R;
import com.test.deencredit.activity_deencredit.ln_deencredit.MainActivity_deencredit;
import com.test.deencredit.base_deencredit.BaseLnFragment_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.RecommendAppInfo_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_deencredit;
import com.test.deencredit.config_deencredit.UserState_deencredit;
import com.test.deencredit.dialog_deencredit.CustomerServiceDialog_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class LnStateFailFragment_deencredit extends BaseLnFragment_deencredit {

    private LinearLayout recommendAppListGroupLayout_deencredit;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page_deencredit = "REJECT";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_ln_state_fail_deencredit, container, false);

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.flsf_refreshlayout);
        TextView textView = view.findViewById(R.id.flsf_tv_text);

        view.findViewById(R.id.flsf_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog_deencredit(getContext()).show());

        refreshLayout.setOnRefreshListener(() -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"REFRESH_CLICK","");
            HttpUtil_deencredit.getInstance_deencredit().userProcess_deencredit(requireActivity(),
                () -> {
                    String state = ProcessInfo_deencredit.getInstance_deencredit().getUserState_deencredit();
                    if (UserState_deencredit.isStateChange_deencredit(state)) {//刷新用户状态
                        FragmentActivity activity = requireActivity();
                        UserState_deencredit.clearState_deencredit();
                        activity.startActivity(new Intent(activity, MainActivity_deencredit.class));
                        activity.finish();
                    }else HttpUtil_deencredit.getInstance_deencredit().requestAppSInfo_deencredit(requireActivity(),this::refreshRecommendAppList_deencredit);
                }
            );
            refreshLayout.setRefreshing(false);
        });

        String failDays = AppShowCustomize_deencredit.getInstance_deencredit().getRejectPeriod_deencredit(requireContext());
        textView.setText(getString(R.string.ln_fail_text_deencredit, failDays));

        recommendAppListGroupLayout_deencredit = view.findViewById(R.id.flsf_layout_recommendapplist);

        refreshRecommendAppList_deencredit();

        return view;
    }

    public void refreshRecommendAppList_deencredit(){
        if (!AppShowCustomize_deencredit.getInstance_deencredit().getRecommendFlag_deencredit()) return;
        recommendAppListGroupLayout_deencredit.removeAllViews();
        ArrayList<RecommendAppInfo_deencredit> recommendAppInfos = AppShowCustomize_deencredit.getInstance_deencredit().getRecommendAppList_deencredit();
        for (RecommendAppInfo_deencredit recommendAppInfo:recommendAppInfos){
            addRecommendApp_deencredit(recommendAppInfo);
        }
//
//        //tag2024
//        HttpUtil_deencredit.getInstance().requestStarWindowConfig(requireActivity(),() -> {
//
//            //1.活动弹窗
//            if (ActiveWindowConfig_deencredit.getInstance().isSwitch() &&
//                    UserInfoUtil_deencredit.getInstance().getActivedialogCount()< ActiveWindowConfig_deencredit.getInstance().getNum()){
//                new RateActiveDialog_deencredit((AppCompatActivity) requireActivity()).show();
//                return;
//            }
//
//            //2.评价弹窗
//            if (NormalWindowConfig_deencredit.getInstance().isSwitch() &&
//                    UserInfoUtil_deencredit.getInstance().getNormaldialogCount()< NormalWindowConfig_deencredit.getInstance().getNum()){
//                new RateNormalDialog_deencredit((AppCompatActivity) requireActivity()).show();
//            }
//
//        });
    }

    private void addRecommendApp_deencredit(RecommendAppInfo_deencredit recommendAppInfo){
        View itemView = LayoutInflater.from(requireContext()).inflate(R.layout.item_recommend_app_deencredit,null);

        TextView titleTv = itemView.findViewById(R.id.ira_tv_title);
        TextView textTv = itemView.findViewById(R.id.ira_tv_text);
        ImageView logoIv = itemView.findViewById(R.id.ira_iv_logo);
        TextView appNameTv = itemView.findViewById(R.id.ira_tv_appname);
        TextView amountTextTv = itemView.findViewById(R.id.ira_tv_amounttext);
        TextView scoreTv = itemView.findViewById(R.id.ira_tv_score);
        TextView downloadsTv = itemView.findViewById(R.id.ira_tv_downloads);

        titleTv.setText(recommendAppInfo.getTitle_deencredit(requireContext()));
        textTv.setText(recommendAppInfo.getText_deencredit(requireContext()));
        appNameTv.setText(recommendAppInfo.getAppName_deencredit());
        amountTextTv.setText(recommendAppInfo.getAmountText_deencredit());
        scoreTv.setText(recommendAppInfo.getScore_deencredit());
        downloadsTv.setText(recommendAppInfo.getDownloads_deencredit());

        //Install按键
        itemView.findViewById(R.id.ira_btn_install).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"RECOMMEND_"+recommendAppInfo.getAppName_deencredit().toUpperCase()+"_INSTALL_CLICK","");
            String path = recommendAppInfo.getPath_deencredit();
            if (!TextUtils.isEmpty(path))
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
        });

        //加载logo
        if (recommendAppInfo.getLogoBitmap_deencredit()==null){
            new Thread(() -> {
                try {
                    String logoPath = recommendAppInfo.getLogo_deencredit();
                    if (TextUtils.isEmpty(logoPath)) return;
                    URL url = new URL(logoPath);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        recommendAppInfo.setLogoBitmap_deencredit(bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(recommendAppInfo.getLogoBitmap_deencredit());

        recommendAppListGroupLayout_deencredit.addView(itemView);
    }



}